package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HChangeOrderDao;
import com.zhongwang.mms.module.half.model.HChangeOrderModel;
import com.zhongwang.mms.module.half.service.ChangeExcService;
import com.zhongwang.mms.module.purchase.model.ChangeOrderQueryModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:串料执行
 * @author: th
 * @time: 2020/5/5 15:29
 */
@Controller
@RequestMapping("/half/halInvManage/changeExc")
public class ChangeExcController extends BaseController {

    //页面文件路径
    private String prefix = "half/halInvManage/changeExc";
    @Autowired
    private ChangeExcService changeExcService;
    @Autowired
    private HChangeOrderDao hChangeOrderDao;

    /**
     * 加载页面
     *
     * @param model
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halexeccha:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 加载页面列表数据
     *
     * @param queryModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:halexeccha:view"})
    public JsonResult list(ChangeOrderQueryModel queryModel) {

        startPage();
        return jsonResult(changeExcService.selectChangeOrderListByQuery(queryModel));
    }

    /**
     * 串料执行
     *
     * @param hChangeOrderModel
     * @return
     */
    @ResponseBody
    @PostMapping("/execApply")
    @RequiresPermissions({"halinv:halexeccha:edit"})
    public JsonResult execApply(HChangeOrderModel hChangeOrderModel) {

        return changeExcService.execApply(hChangeOrderModel);
    }

    /**
     * 驳回串料申请
     *
     * @param hChangeOrderModel
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions({"halinv:halexeccha:edit"})
    public JsonResult remove(HChangeOrderModel hChangeOrderModel) {

        if (changeExcService.remove(hChangeOrderModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * @param model
     * @param mode
     * @param ceoId
     * @return
     */
    @GetMapping("/viewPage/{mode}/{ceoId}")
    public String viewPage(Model model, @PathVariable("mode") String mode, @PathVariable("ceoId") Integer ceoId) {

        HChangeOrderModel hChangeOrderModel = hChangeOrderDao.getAllByPrimary(ceoId);
        model.addAttribute("mode", mode);
        model.addAttribute("ceoId", hChangeOrderModel.getCeoId());
        model.addAttribute("ceoAppPerson", hChangeOrderModel.getCeoAppPerson());
        model.addAttribute("ceoAppPersonId", hChangeOrderModel.getCeoAppPersonId());
        model.addAttribute("ceoAppDate", new SimpleDateFormat("yyyy-MM-dd").format(hChangeOrderModel.getCeoAppDate()));
        model.addAttribute("ceoNeedProCode", hChangeOrderModel.getCeoNeedProCode());
        model.addAttribute("ceoNeedProName", hChangeOrderModel.getCeoNeedProName());
        model.addAttribute("ceoNeedMatName", hChangeOrderModel.getCeoNeedMatName());
        model.addAttribute("ceoNeedNum", hChangeOrderModel.getCeoNeedNum());
        model.addAttribute("ceoNeedUnit", hChangeOrderModel.getCeoNeedUnit());
        model.addAttribute("ceoUseProCode", hChangeOrderModel.getCeoUseProCode());
        model.addAttribute("ceoUseProName", hChangeOrderModel.getCeoUseProName());
        model.addAttribute("ceoUseMatName", hChangeOrderModel.getCeoUseMatName());
        model.addAttribute("ceoUseNum", hChangeOrderModel.getCeoUseNum());
        model.addAttribute("ceoUseUnit", hChangeOrderModel.getCeoUseUnit());
        return "half/halInvManage/changeApply/createPage";
    }

    /**
     * 撤销
     *
     * @param hChangeOrderModel
     * @return
     */
    @ResponseBody
    @PostMapping("/revokeApply")
    @RequiresPermissions({"halinv:halexeccha:edit"})
    public JsonResult revokeApply(HChangeOrderModel hChangeOrderModel) {

        if (changeExcService.revokeApply(hChangeOrderModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail("因串料需求的串料库存信息因被标记或库存不足导致无法撤销！");
    }
}
